package com.lv.common.utils;

import android.animation.ValueAnimator;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.RotateAnimation;
import android.widget.ImageView;

import com.lv.common.R;


/**
 * 创建者：ygx
 * 创建日期：2021/6/21 10:02 AM
 * 描述：TODO
 */
public class AnimationUtils {
    /**
     * 将view从不可见变为可见的动画，原理:动态改变其LayoutParams.height的值
     *
     * @param view 要展示动画的view
     */
    public static void visibleAnimator(final View view) {
        if (view != null) {
            int viewHeight = view.getHeight();
//            viewHeight = 50;
            if (viewHeight == 0) {
                int width = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
                int height = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
                view.measure(width, height);
                viewHeight = view.getMeasuredHeight();
            }
            ValueAnimator animator = ValueAnimator.ofInt(0, viewHeight);
//            animator.setDuration(3000);
            animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                @Override
                public void onAnimationUpdate(ValueAnimator animation) {
                    ViewGroup.LayoutParams params = view.getLayoutParams();
                    params.height = (int) animation.getAnimatedValue();
                    view.setLayoutParams(params);
                }
            });
            animator.start();
        }
    }

    public static void visibleAnimator2(final View view) {
        if (view != null) {
            int viewHeight = view.getHeight();
            if (viewHeight == 0) {
                int width = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
                int height = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
                view.measure(width, height);
                viewHeight = view.getMeasuredHeight();
            }
            ValueAnimator animator = ValueAnimator.ofInt(0, viewHeight);
//            animator.setDuration(3000);
            animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                @Override
                public void onAnimationUpdate(ValueAnimator animation) {
                    ViewGroup.LayoutParams params = view.getLayoutParams();
                    params.height = (int) animation.getAnimatedValue();
                    view.setLayoutParams(params);
                }
            });
            animator.start();
        }
    }

    /**
     * 将view从不可见变为可见的动画，原理:动态改变其LayoutParams.width的值
     *
     * @param view 要展示动画的view
     */
    public static void visibleAnimatorWidth(final View view) {
        if (view != null) {
            int viewWidth = view.getWidth();
            if (viewWidth == 0) {
                int width = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
                int height = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
                view.measure(width, height);
                viewWidth = view.getWidth();
            }
            ValueAnimator animator = ValueAnimator.ofInt(0, viewWidth);
            animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                @Override
                public void onAnimationUpdate(ValueAnimator animation) {
                    ViewGroup.LayoutParams params = view.getLayoutParams();
                    params.width = (int) animation.getAnimatedValue();
                    view.setLayoutParams(params);
                }
            });
            animator.start();
        }
    }


    /**
     * 将view从可见变为不可见的动画，原理:动态改变其LayoutParams.height的值
     *
     * @param view 要展示动画的view
     */
    public static void invisibleAnimator(final View view) {
        if (view != null) {
            int viewHeight = view.getHeight();
//            viewHeight = 50;
//            viewHeight = DensityUtil.dip2px(view.getContext(), viewHeight);
            if (viewHeight == 0) {
                int width = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
                int height = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
                view.measure(width, height);
                viewHeight = view.getMeasuredHeight();
            }
            ValueAnimator animator = ValueAnimator.ofInt(viewHeight, 0);
            animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                @Override
                public void onAnimationUpdate(ValueAnimator animation) {
                    ViewGroup.LayoutParams params = view.getLayoutParams();
                    params.height = (int) animation.getAnimatedValue();
                    view.setLayoutParams(params);
                }
            });
            animator.start();
        }
    }

    public static void invisibleAnimator2(final View view) {
        if (view != null) {
            int viewHeight = view.getHeight();
//            viewHeight = DensityUtil.dip2px(view.getContext(), viewHeight);
            if (viewHeight == 0) {
                int width = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
                int height = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
                view.measure(width, height);
                viewHeight = view.getMeasuredHeight();
            }
            ValueAnimator animator = ValueAnimator.ofInt(viewHeight, 0);
            animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                @Override
                public void onAnimationUpdate(ValueAnimator animation) {
                    ViewGroup.LayoutParams params = view.getLayoutParams();
                    params.height = (int) animation.getAnimatedValue();
                    view.setLayoutParams(params);
                }
            });
            animator.start();
        }
    }


    /**
     * 将view从可见变为不可见的动画，原理:动态改变其LayoutParams.width的值
     *
     * @param view 要展示动画的view
     */
    public static void invisibleAnimatorW(final View view) {
        if (view != null) {
            int viewWidth = view.getWidth();
            if (viewWidth == 0) {
                int width = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
                int height = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
                view.measure(width, height);
                viewWidth = view.getWidth();
            }
            ValueAnimator animator = ValueAnimator.ofInt(viewWidth, 0);
            animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                @Override
                public void onAnimationUpdate(ValueAnimator animation) {
                    ViewGroup.LayoutParams params = view.getLayoutParams();
                    params.width = (int) animation.getAnimatedValue();
                    view.setLayoutParams(params);
                }
            });
            animator.start();
        }
    }

    /**
     * 动态改变view的高度动画效果，动画时长300毫秒[android属性动画默认时长]
     * 原理:动画改变view LayoutParams.height的值
     *
     * @param view        要进行高度改变动画的view
     * @param startHeight 动画前的view的高度
     * @param endHeight   动画后的view的高度
     */
    public static void changeViewHeightAnimatorStart(final View view, final int startHeight, final int endHeight) {
        if (view != null && startHeight >= 0 && endHeight >= 0) {
            ValueAnimator animator = ValueAnimator.ofInt(startHeight, endHeight);
            animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                @Override
                public void onAnimationUpdate(ValueAnimator animation) {
                    ViewGroup.LayoutParams params = view.getLayoutParams();
                    params.height = (int) animation.getAnimatedValue();
                    view.setLayoutParams(params);
                }
            });
            animator.start();
        }
    }

    /**
     * 控件的抖动动画
     *
     * @param v
     */
    public static void setHintAnimation(View v) {
        v.requestFocus();
        Animation animation = android.view.animation.AnimationUtils.loadAnimation(v.getContext(), R.anim.shake);
        v.startAnimation(animation);
    }

    public static void animationIvOpen(ImageView iv_show) {
        RotateAnimation animation = new RotateAnimation(0, 180,
                Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,
                0.5f);
        animation.setFillAfter(true);
        animation.setDuration(100);
        iv_show.startAnimation(animation);
    }

    public static void animationIvClose(ImageView iv_show) {
        RotateAnimation animation = new RotateAnimation(180, 0,
                Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,
                0.5f);
        animation.setFillAfter(true);
        animation.setDuration(100);
        iv_show.startAnimation(animation);
    }

}
